var express = require('express');
var router = express.Router();
var async = require('async');
var multiparty = require('multiparty');
var MongoClient = require('mongodb').MongoClient;
var fs = require('fs');
var DB_CONN_STR = "mongodb://localhost:27017/douban";
// router.get('/', function (req, res, next) {
//   res.render('index', { title: '豆瓣' ,username:req.session.username});
// });

router.post('/uploadImg',function(req,res,next){
  var form = new multiparty.Form();
  form.encoding = 'utf-8';
  form.uploadDir = './uploadtmp';
  form.maxFilesSize = 10000*1024;



  form.parse(req,function(err,fields,files){
  // 就是取files对象里面的一些信息内容而已
  // 设置了一个目录上传的新的文件名结构以及路径地址
  
   var uploadurl = '/images/upload/';
   var file = files['filedata'];
  

   var originalFilename = file[0].originalFilename;
   var tmpPath = file[0].path;

   var timestamp = new Date().getTime();
   uploadurl += timestamp + originalFilename;
   var newPath = './public' + uploadurl;
   // public/images/upload/12123123213logo.jpg


   // 对原文件内容进行一个流文件信息的读取
   var fileReadStream = fs.createReadStream(tmpPath);
   var fileWriteStream = fs.createWriteStream(newPath);
   fileReadStream.pipe(fileWriteStream);
   fileWriteStream.on('close',function(){
     fs.unlinkSync(tmpPath);
    //  {"err":"","msg":"200906030521128703.gif"}
    res.send('{"err":"","msg":"'+ uploadurl +'"}')
   })



  })

})



router.get('/login', function (req, res, next) {
  res.render('login', {});
});
router.get('/registor', function (req, res, next) {
  res.render('registor', {});
});
router.get('/logout',function(req,res,next){
  req.session.username = undefined;
  res.redirect('/')
});

router.get('/', function(req, res, next) {
 
  async.parallel([
       function(cb){
         // 轮播图的查询
         MongoClient.connect(DB_CONN_STR, function (err, db) {
          if (err) {
            return;
          } else {
            var conn = db.collection('coming_soon');
            var data = {}
            conn.find(data).limit(5).toArray(function(err,results){
              cb(null,results);
            })
          }
        })
       },
       function(cb){
         // coming_soon
         MongoClient.connect(DB_CONN_STR, function (err, db) {
          if (err) {
            return;
          } else {
            var conn = db.collection('in_theaters');
            var data = {}
            conn.find(data).limit(11).toArray(function(err,results){
              cb(null,results);
            })
          }
        })
       },
       function(cb){
         // top250
         MongoClient.connect(DB_CONN_STR, function (err, db) {
          if (err) {
            return;
          } else {
            var conn = db.collection('top250');
            var data = {}
            conn.find(data).limit(12).toArray(function(err,results){
              cb(null,results);
            })
          }
        })
       },
       function(cb){
         // coming_soon
         MongoClient.connect(DB_CONN_STR, function (err, db) {
          if (err) {
            return;
          } else {
            var conn = db.collection('coming_soon');
            var data = {}
            conn.find(data).limit(6).toArray(function(err,results){
              cb(null,results);
            })
          }
        })
       },
       function(cb){
         // us_box
         MongoClient.connect(DB_CONN_STR, function (err, db) {
          if (err) {
            return;
          } else {
            var conn = db.collection('us_box');
            var data = {}
            conn.find(data).limit(6).toArray(function(err,results){
              cb(null,results);
            })
          }
        })
       }
     ],function(err,results){
         res.render('index', { douban: {
          carousel:results[0],
          in_theaters:results[1],
          top250: results[2],
          comingSoon: results[3],
          us_box: results[4],
          username:req.session.username
        } });
     })

 
});
module.exports = router;

